package mi.payment.transaction.authorize.base;

import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import lainey.ui.widget.TitleView;
import mi.payment.R;
import mi.payment.settings.TransactionSwitchOperation;
import mi.payment.settings.TransactionSwitchSetting;
import mi.payment.transaction.BaseMenuActivity;
import mi.payment.transaction.authorize.PreAuthorizeInputMoneyActivity;
import mi.payment.transaction.authorize.complete.PreAuthorizeCompleteSwingActivity;
import mi.payment.transaction.authorize.complete.revoke.PreAuthorizeCompleteRevokeActivity;
import mi.payment.transaction.authorize.revoke.PreAuthorizeRevokeInputAdminPwdActivity;
import mi.payment.transaction.sale.HomeInputMoneyActivity;
import mi.payment.ui.other.LoginActivity;
import mi.payment.utils.SettingHomePageUtils;
import mi.payment.utils.transaction.ParamsInitTask;

/**
 * 预授权首页
 */
public class PreAuthorizeMainActivity extends BaseMenuActivity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        initView();

        if (ParamsInitTask.isChecked) {
            SettingHomePageUtils.getInstance().setHomeActivity(this);
            return;
        }

        boolean isAuto = TransactionSwitchOperation.getInstance().getTransactionSwitchSetting().isAutoChecked();
        if (isAuto) {
            mParamsCheckExt.autoLoginAndSign();
        } else {
            openActivity(LoginActivity.class);
        }
    }

    @Override
    public void sendCloseAppBroadcast() {
        super.sendCloseAppBroadcast();
        SettingHomePageUtils.goPre = true;
    }

    @Override
    public View getView() {
        return View.inflate(this, R.layout.authorize_activity_main, null);
    }

    private void initView() {
        TitleView titleView = findViewById(R.id.title_view);
        titleView.setCenterText(R.string.menu_auth);
        titleView.setLeftImageResource(R.drawable.ic_menu);

        Drawable drawable = getDrawable(R.drawable.ic_cashier);
        int minimumWidth = drawable.getMinimumWidth();
        int minimumHeight = drawable.getMinimumHeight();
        drawable.setBounds(0, 0, minimumWidth, minimumHeight);

        TextView rightText = titleView.getRightText();
        rightText.setText(R.string.menu_sale);
        rightText.setVisibility(View.VISIBLE);
        rightText.setCompoundDrawablePadding(8);
        rightText.setCompoundDrawables(drawable, null, null, null);

        titleView.setLeftImageOnClickListener(
                v -> openMenu()
        );
        rightText.setOnClickListener(
                v -> {
                    openActivity(HomeInputMoneyActivity.class);
                    finish();
                }
        );

        // 预授权
        View item = findViewById(R.id.item_pre_auth);
        ImageView leftImage = item.findViewById(R.id.left_image);
        leftImage.setImageResource(R.drawable.pre_auth);
        TextView leftText = item.findViewById(R.id.left_text);
        leftText.setText(R.string.pre_auth);
        item.setOnClickListener(this);

        // 预授权撤销
        item = findViewById(R.id.item_pre_auth_revoke);
        leftImage = item.findViewById(R.id.left_image);
        leftImage.setImageResource(R.drawable.pre_auth_revoke);
        leftText = item.findViewById(R.id.left_text);
        leftText.setText(R.string.pre_auth_revoke);
        item.setOnClickListener(this);

        // 预授权完成
        item = findViewById(R.id.item_pre_auth_complete);
        leftImage = item.findViewById(R.id.left_image);
        leftImage.setImageResource(R.drawable.pre_auth_complete);
        leftText = item.findViewById(R.id.left_text);
        leftText.setText(R.string.pre_auth_complete);
        item.setOnClickListener(this);

        // 预授权完成撤销
        item = findViewById(R.id.item_pre_auth_complete_revoke);
        leftImage = item.findViewById(R.id.left_image);
        leftImage.setImageResource(R.drawable.pre_auth_complete_revoke);
        leftText = item.findViewById(R.id.left_text);
        leftText.setText(R.string.pre_auth_complete_revoke);
        item.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        final int id = v.getId();

        boolean isCheck = mParamsCheckExt.check(true, true, true, true);
        if (isCheck) return;

        TransactionSwitchSetting transactionSwitchSetting = TransactionSwitchOperation.getInstance().getTransactionSwitchSetting();
        switch (id) {
            case R.id.item_pre_auth:
                boolean isFieldAuth = transactionSwitchSetting.isFieldAuth();
                if (isFieldAuth) {
                    PreAuthorizeInputMoneyActivity.startAction(this, false);
                } else {
                    showTextHintDialog(R.string.error_no_support);
                }
                break;
            case R.id.item_pre_auth_revoke:
                boolean isFieldAuthRevoke = transactionSwitchSetting.isFieldAuthRevoke();
                if (isFieldAuthRevoke) {
                    PreAuthorizeRevokeInputAdminPwdActivity.startAction(this);
                } else {
                    showTextHintDialog(R.string.error_no_support);
                }
                break;
            case R.id.item_pre_auth_complete:
                boolean isFieldAuthReq = transactionSwitchSetting.isFieldAuthReq();
                if (isFieldAuthReq) {
                    PreAuthorizeCompleteSwingActivity.startAction(this);
                } else {
                    showTextHintDialog(R.string.error_no_support);
                }
                break;
            case R.id.item_pre_auth_complete_revoke:
                boolean isFieldAuthCompleteRevocation = transactionSwitchSetting.isFieldAuthCompleteRevocation();
                if (isFieldAuthCompleteRevocation) {
                    openActivity(PreAuthorizeCompleteRevokeActivity.class);
                } else {
                    showTextHintDialog(R.string.error_no_support);
                }
                break;
        }
    }


}
